package com.rdxer.myblog.api;

import com.rdxer.myblog.api.para.LoginPara;
import com.rdxer.myblog.api.para.RegisterPara;
import com.rdxer.myblog.model.Account;
import com.rdxer.myblog.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@CrossOrigin
@RestController
public class AuthApi {

    @Autowired
    AuthService authService;

    @PostMapping("/login")
    public String login(@RequestBody LoginPara para){
        String token = authService.login(para.getUsername(), para.getPassword());
//        JpaRepository
        return token;
    }
    @PostMapping("/register")
    public String register(@RequestBody RegisterPara para){
        authService.register(para.getUsername(),para.getDisplayName(),para.getPassword());
//        JpaRepository
        return "注册成功";
    }

    // 获取用户信息
    @GetMapping("/userinfo")
    public Account userinfo(@RequestHeader Map<String,String> header){
//        authService.userinfo();
//        JpaRepository
        String token = header.get("token");
        System.out.println(token);

        Account userinfo = authService.userinfo(token);
        userinfo.setPassword(null);
        return userinfo;

    }





    // 参数发送 1.
    @PostMapping("/login1")
    public String login1(@RequestBody LoginPara para){
        return "登陆" + para.getUsername();
    }
    // 参数发送 2.
    @PostMapping("/test/{id}")
    public String login21( @PathVariable String id){
        return "" +id;
    }
    // 参数发送 3.
    @PostMapping("/test2")
    public String login2122(String id,String id2){
        return "" +id  + " - " + id2;
    }
}
